package priv.pront.code.national.acwing.dp.bags;

import java.util.Scanner;

public class Acwing1022_收服宠物小精灵 {

    static int n, m, k, hit = 0;
    static int[] hurt, num;
    static int[][] dp;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt(); //精灵球数量
        m = scanner.nextInt();  //皮卡丘初始的体力值
        k = scanner.nextInt();  //野生小精灵的数量
        hurt = new int[k + 1];
        num = new int[k + 1];
        dp = new int[1005][505];
        for (int i = 1; i <= k; i++) {
            num[i] = scanner.nextInt();
            hurt[i] = scanner.nextInt();
        }
        for (int i = 1; i <= k; i++) {
            for (int j = n; j >= 1; j--) {  // 剩余的精灵球数量
                for (int t = m; t >= 1; k--) {  // 剩余体力值
                    // 捕捉
                    if (j - num[i] >= 0 && t - hurt[i] >= 0) {
                        if (dp[j][t] == dp[j - num[i]][t - hurt[i]]) {
                            dp[j][t] = t > t - hurt[i] ? dp[j][t] : dp[j - num[i]][t - hurt[i]];
                        } else {
                            dp[j][t] = Math.max(dp[j][t], dp[j - num[i]][t - hurt[i]]);
                        }
                    }

                }
            }
        }
        System.out.println(dp[n][m]);
    }
}
